$(function () {
    // 打开与关闭事件绑定
    $('.admin_id .add_btn input').click(function () {
        $('.admin_id .add_bg').css('display', 'block');
    });
    $('.admin_id .add_bg i').click(function () {
        $('.admin_id .add_bg').css('display', 'none');
    })

    // 添加按钮
    $('.admin_id .add_bg .add_page .button').click(function () {
        $('.admin_id .add_bg .add_page .error').html('');
        var txtval = $('.admin_id .add_bg .add_page .text');
        if (txtval.val() == null || txtval.val() == '') {
            $('.admin_id .add_bg .add_page .error').html('授权id为空！！！');
        } else {
            $.ajax({
                type: "get",
                url: "adminid?action=add&passid=" + txtval.val(),
                async: false,
                success: function (date) {
                    if (date == 1) {
                        $('.admin_id .add_bg .add_page .error').html('授权id存在！！！');
                    } else if (date == 2) {
                        $('.admin_id .add_bg .add_page .error').html('添加成功！！！');
                        txtval.val('');
                    } else if (date == 3) {
                        $('.admin_id .add_bg .add_page .error').html('添加失败！！！');
                        txtval.val('');
                    }
                }
            });
        }
    });


    // // 是否在查询中标志
    // var flag = false;
    // // 记录搜索框的值
    // var search_value = '';

    //总页面数
    function totalPage() {
        var total = 0;
        // ajax同步请求
        $.ajax({
            type: "get",
            url: "adminid?action=totalPage",
            async: false,
            success: function (date) {
                total = date;
            }
        });
        return total;
    }
    var totalpage = totalPage();

    // 页面加载
    var nowpage = 1;
    function showPage(nowpage) {
        $.ajax({
            type: "get",
            url: "adminid?action=changePage&page=" + nowpage,
            dataType: "json",
            success: function (date) {
                add(date);
            }
        });
        function add(data) {
            var trArraay = $('table tbody').children();
            for (var i = 0; i < trArraay.length; i++) {
                trArraay[i].remove();
            }
            for (var i = 0; i < data.length; i++) {
                var index = i + 1 + (nowpage - 1) * 10;
                var passid = data[i].passid;
                var state = data[i].state == 0 ? '未添加' : '已添加';
                var username = data[i].username;
                var adddate = data[i].adddate;
                var bindingdate = data[i].state == 0 ? '' : data[i].bindingdate;
                var newNode = $("<tr><td>" + index + "</td><td>" + passid + "</td><td>" + state + "</td><td>" + username + "</td><td>" + adddate + "</td><td>" + bindingdate + "</td></tr>");
                $('table').append(newNode);
            }
        }
    }
    showPage(nowpage);

    // 查找页面加载
    function searchPage(nowpage, username) {
        $.ajax({
            type: "get",
            url: "ajax?action=searchPage&page=" + nowpage + "&name=" + username,
            dataType: "json",
            success: function (date) {
                add(date);
            }
        });
        function add(data) {
            var trArraay = $('table tbody').children();
            for (var i = 0; i < trArraay.length; i++) {
                trArraay[i].remove();
            }
            for (var i = 0; i < data.length; i++) {
                var id = data[i].id;
                var name = data[i].name;
                var gender = data[i].gender;
                var email = data[i].email;
                var newNode = $("<tr><td>" + id + "</td><td>" + name + "</td><td>" + gender + "</td><td>" + email + "</td></tr>");
                $('table').append(newNode);
            }
        }
    }

    // 按钮加载
    function showButton() {
        var append_html = '<input type="button" value="上一页" class="prev_page">';
        for (var i = 0; i < totalpage - 1; i++) {
            if (totalpage > 8 && nowpage > 5 && i < nowpage - 3) {
                if (i < 2) {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                }
                else if (i == 2) {
                    append_html += '<span class="page_dot">•••</span>';
                }
            } else if (totalpage > 8 && nowpage < totalpage - 3 && i > nowpage + 1) {
                if (nowpage > 5 && i == nowpage + 2) {
                    append_html += '<span class="page_dot">•••</span>';
                } else if (nowpage < 7) {
                    if (i < 7) {
                        append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                    } else if (i == 7) {
                        append_html += '<span class="page_dot">•••</span>';
                    }
                }
            }

            else {
                if (i == nowpage - 1) {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number nowpage">';
                } else {
                    append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
                }
            }
        }

        if (nowpage == totalpage) {
            append_html += '<input type="button" value="' + (i + 1) + '" class="number nowpage">';
        } else {
            append_html += '<input type="button" value="' + (i + 1) + '" class="number">';
        }
        append_html += '<input type="button" value="下一页" class="next_page"><span>共' + totalpage + '页,到第</span><input type="text" class="targetPage"><span>页</span><input type="button" value="确定" class="ok">';
        $('.page').append(append_html);
    }
    showButton();

    $('.page').on('click', 'input', function () {

        // 上一页
        if ($(this).hasClass('prev_page')) {
            nowpage--;
            if (nowpage < 1) {
                nowpage = 1
            }
            showPage(nowpage);
            $('.page').html('');
            showButton();

            // if (!flag) {

            // } else {
            //     searchPage(nowpage, search_value);
            //     $('.page').html('');
            //     showButton();
            // }
            return;
        }
        // 下一页
        else if ($(this).hasClass('next_page')) {
            nowpage++;
            if (nowpage > totalpage) {
                nowpage = totalpage;
            }
            showPage(nowpage);
            $('.page').html('');
            showButton();

            // if (!flag) {
            // } else {
            //     searchPage(nowpage, search_value);
            //     $('.page').html('');
            //     showButton();
            // }
            return;
        }
        // 跳转页按钮
        else if ($(this).hasClass('ok') && !$(this).hasClass('nowpage')) {
            nowpage = parseInt($('.targetPage').val());
            console.log($('.targetPage').val());
            if ($('.targetPage').val() == null || $('.targetPage').val() == '') {
                nowpage = 1;
            }
            if (nowpage > totalpage) {
                nowpage = totalpage;
            }
            showPage(nowpage);
            $('.page').html('');
            showButton();
            // if (!flag) {
            // } else {
            //     searchPage(nowpage, search_value);
            //     $('.page').html('');
            //     showButton();
            // }
            return;
        }
        //点击页码事件
        else if ($(this).hasClass('number')) {
            nowpage = parseInt($(this).val());
            showPage(nowpage);
            $('.page').html('');
            showButton();
            // if (!flag) {
            // } else {
            //     searchPage(nowpage, search_value);
            //     $('.page').html('');
            //     showButton();
            // }
            return;
        }
    });

    // 搜索按钮
    // $('.table .search .search_btn').click(function () {
    //     search_value = $('.search_text').val();
    //     nowpage = 1;
    //     if (search_value == null || search_value == '') {
    //         flag = false;
    //         showPage(nowpage);
    //         totalpage = totalPage();
    //         $('.page').html('');
    //         showButton();
    //     } else {
    //         flag = true;
    //         searchPage(nowpage, search_value);
    //         $.ajax({
    //             type: "get",
    //             url: "ajax?action=totalSearch&name=" + search_value,
    //             async: false,
    //             success: function (date) {
    //                 totalpage = date;
    //             }
    //         });
    //         if (totalpage == 0) {
    //             $('.page').html('');
    //         } else {
    //             $('.page').html('');
    //             showButton();
    //         }
    //     }
    // });

});